// pages/mine/mine.js
const adapters = require("../../utils/adapters");
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    header: {
      title: '长兴诺力售后',
      whiteBg: false,
      mode: 'title',
      navBarHeight: app.globalData.navBarHeight
    },
    tarBarActive: 1,
    userInfo: null,
    waitmassageNumber: 0,
    repliedMessageNumber: 0,
    waitSendOutNumber: 0,
  },

  tarBarChange() {
    this.setData({tarBarActive: 1})
  },

  onLoad(options) {
    this.setData({
      userInfo: wx.getStorageSync('userInfo'),
    });
  },
  goMine() {
    wx.navigateTo({
      url: '/pages/editMine/editMine',
    })
  },
  goShop(){
    wx.navigateTo({
      url: '/pages/pointsShop/pointsShop',
    })
  },
  navigateToListPage(e) {
    let type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: `/pages/workOrderList/workOrderList?pageMode=${type}`,
    })
  },

  goPoints() {
    wx.navigateTo({
      url: '/pages/points/points',
    })
  },

  goAddress() {
    wx.navigateTo({
      url: '/pages/address/address',
    })
  },


  async onShow() {
    const userInfo = this.data.userInfo;
  
    try {
      // 并行请求所有类型的工单列表
      const [waitSendOutRes, waitmassageRes, repliedMessageRes] = await Promise.all([
        adapters.getMaintenanceList({ type: 0, phoneNumber: userInfo.phoneNumber }),
        adapters.getMaintenanceList({ type: 1, phoneNumber: userInfo.phoneNumber }),
        adapters.getMaintenanceList({ type: 2, phoneNumber: userInfo.phoneNumber })
      ]);
  
      // 进行空值处理
      const waitSendOutList = waitSendOutRes.data || [];
      const waitmassageList = waitmassageRes.data || [];
      const repliedMessageList = repliedMessageRes.data || [];
  
      // 更新工单数量到页面上
      this.setData({
        waitSendOutNumber: waitSendOutList.length,
        waitmassageNumber: waitmassageList.length,
        repliedMessageNumber: repliedMessageList.length,
      });
  
    } catch (err) {
      console.error('获取维修列表失败:', err);
      wx.showToast({
        title: '数据加载失败',
        icon: 'none'
      });
    }
  }
})